package com.example.administrator.myapplication;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private Button setfontSizeBt;
    private TextView contentTv;
     int textSize = 1;
    AlertDialog alertDialog;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        ininData();
        initEvent();
    }
    private  void initView(){
        contentTv = findViewById(R.id.tv);
        setfontSizeBt = findViewById(R.id.bt);

    }
    private  void ininData(){

    }
    private  void  initEvent(){
        setfontSizeBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setAlertDialogFontSize();
            }
        });
    }
    //设置弹出框为单选对话框的内容
    private  void setAlertDialogFontSize(){
        final int[] textSizeArr = {10,20,25,30,40};

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("设置字体大小");
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setSingleChoiceItems(new String[]{"小号", "默认", "中号", "大号", "超大"}, textSize, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                textSize = i;
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                contentTv.setTextSize(textSizeArr[textSize]);
                alertDialog.dismiss();//关闭对话框
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                alertDialog.dismiss();
            }
        });
        alertDialog = builder.create(); //根据builer创建Alertialog
        alertDialog.show();
    }
}
